package com.sfpay.coreplatform.order.service.composite.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.common.enums.TradeType;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;
import com.sfpay.coreplatform.order.valueobject.tmo.TradeOrder;

@Service
public class CheckCancelHandler extends AbstractHandler {

	@Override
	public Response Handler(PayRequest request, PayContext payContext)
			throws OrderServiceException {
		Response response = new Response();
		List<TradeOrder> trades = queryOrderService.queryPayTradeByOutTradeNo(
				request.getPlatformMemberNo(), request.getOutTradeNo());

		if (trades == null || trades.size() == 0) {
			response.setReturnCode(OrderResponseCode.ORDER_PAYORDER_ISNOT_EXIST);
			response.setReturnMsg("method:cancelPay businessNo:"
					+ request.getOutTradeNo() + " is not exist");
			return response;
		}

		int size = trades.size();
		if (size == 1) {// 只存在一条记录

			TradeOrder trade = trades.get(0);
			if (!(TradeType.PAY == trade.getTradeType() && tradeStatus.SUCCESS == trade
					.getOrderStatus())) {// 支付类型交易失败

				response.setReturnCode(OrderResponseCode.ORDER_OTHER_EXCEPTION);
				response.setReturnMsg("method:cancelPay businessNo:"
						+ request.getOutTradeNo()
						+ " status is failure,not cancel");
				return response;
			}

		}

		// 已经存撤消
		if (trades.size() > 1) {
			for (TradeOrder trade : trades) {

				if (TradeType.CANCEL == trade.getTradeType()) {

					if (tradeStatus.FAILURE == trade.getOrderStatus()) {

						tradeStatus status = getTradeStatus(trades,
								TradeType.REVERSE);
						if (status != null && status == tradeStatus.SUCCESS) {// 已经冲正成功
																				// 不能撤消

							response.setReturnCode(OrderResponseCode.ORDER_ALREADY_REVERSE);
							response.setReturnMsg("method:cancelPay businessNo:"
									+ request.getOutTradeNo()
									+ " already reverse success,not cancel");
							return response;
						}
					} else {
						response.setReturnCode(OrderResponseCode.ORDER_ALREADY_CANCEL);
						response.setReturnMsg("method:cancelPay businessNo:"
								+ request.getOutTradeNo()
								+ " already cancel success");
						return response;
					}
					break;
				} else if (TradeType.REVERSE == trade.getTradeType()
						&& tradeStatus.SUCCESS == trade.getOrderStatus()) {

					response.setReturnCode(OrderResponseCode.ORDER_ALREADY_REVERSE);
					response.setReturnMsg("method:cancelPay businessNo:"
							+ request.getOutTradeNo()
							+ " already reverse success,not cancel");
					return response;

				}
			}

		}

		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);
		return response;
	}

	@Override
	public String getHandlerName() {
		 
		return "CheckCancelHandler";
	}

}
